Page({
  data: {
    idCardLast4: '',
    verificationCode: '',
    timer: "", // 定时器名字
    countDownNum: '60', // 倒计时初始值
  },
  /**
   * 验证码倒计时
   */
  countDown: function () {
    var _this = this;
    var countDownNum = _this.data.countDownNum; // 获取倒计时初始值
    var timer = setInterval(function () {
      countDownNum -= 1;
      _this.setData({
        countDownNum: countDownNum
      })
      if (countDownNum <= -1) {
        clearInterval(timer);
        // 取消置顶的setInterval函数将要执行的代码
        _this.setData({
          countDownNum: 60,
        })
      }
    }, 1000)
  },

  onIdCardInput(e) {
    this.setData({
      idCardLast4: e.detail.value
    });
  },

  onCodeInput(e) {
    this.setData({
      verificationCode: e.detail.value
    });
  },

  sendVerificationCode() {
    const {
      idCardLast4
    } = this.data;

    if (idCardLast4.trim() === '') {
      wx.showToast({
        icon: 'none',
        title: '身份证号后四位不能为空',
      });
      return;
    }

    // 这里可以添加请求发送验证码的逻辑
    wx.showToast({
      title: '验证码已发送',
    });
  },

  submitVerification() {
    const {
      idCardLast4,
      verificationCode
    } = this.data;

    if (idCardLast4.trim() === '') {
      wx.showToast({
        icon: 'none',
        title: '身份证号后四位不能为空',
      });
    } else if (verificationCode.trim() === '') {
      wx.showToast({
        icon: 'none',
        title: '验证码不能为空',
      });
    } else {
      // 这里可以添加提交验证码验证的逻辑
      wx.showToast({
        title: '验证成功',
      });
    }
  },
  login() {
    // 判断验证码是否正确
    // 后端请求12306，若登录成功，则跳转，否则提示登录失败
    if (true) {
      wx.navigateTo({
        url: '/pages/search/search',
      })
    } else {
    }
  }
});